package com.ruoyi.produce.process.bulletin.service;

import com.ruoyi.produce.base.shift.service.IProduceShiftInfoService;
import com.ruoyi.produce.process.bulletin.domain.Achievement;
import com.ruoyi.produce.process.bulletin.domain.AchievementInfo;
import com.ruoyi.produce.process.bulletin.domain.Mold;
import com.ruoyi.produce.process.bulletin.domain.Packing;
import com.ruoyi.produce.process.bulletin.mapper.BulletinMapper;
import com.ruoyi.produce.process.scheduling.domain.Scheduling;
import com.ruoyi.produce.process.scheduling.mapper.SchedulingMapper;
import com.ruoyi.produce.process.worksheet.domain.ProduceWorkSheet;
import com.ruoyi.produce.process.worksheet.service.IProduceWorkSheetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author Drj 2021/12/18
 */
@Service
public class BulletinServiceImpl implements IBulletinService {
    @Autowired
    private BulletinMapper bulletinMapper;
    @Autowired
    private IProduceShiftInfoService produceShiftInfoService;
    @Autowired
    private SchedulingMapper schedulingMapper;
    @Autowired
    private IProduceWorkSheetService workSheetService;

    @Override
    public List<Mold> selectMoldListNew() {
        List<Mold> moldList = bulletinMapper.selectMoldListNew();
        moldList.removeIf(item -> item.geteShiftInfoId() == null && item.getmShiftInfoId() == null);
        return moldList;
    }

    /**
     * 注塑车间看板
     *
     * @return
     */
    @Override
    public List<Mold> selectMoldList() {
        return bulletinMapper.selectMoldList();
    }

    /**
     * 查看包装车间看板
     * @return
     */
    @Override
    public List<Packing> selectPackingList() {
        List<Packing> packingList = new ArrayList<>(); // 返回数组
        String dayShiftId = produceShiftInfoService.createDayShiftId(); //白班编号
        String nightShiftId = produceShiftInfoService.createNightShiftId(); //晚班编号
//        System.out.println("过滤前的加工单" + schedulingMapper.selectSchedulingByShiftInfoId(dayShiftId));
        List<Scheduling> daySchedulingList = filterPackingWorkSheet(schedulingMapper.selectSchedulingByShiftInfoId(dayShiftId)); //白班包装车间的排产记录
//        System.out.println("过滤之后的白班加工单" + daySchedulingList);
        List<Scheduling> nightSchedulingList = filterPackingWorkSheet(schedulingMapper.selectSchedulingByShiftInfoId(nightShiftId)); //晚班包装车间的排产记录
        List<Packing> dayPackingList = createPackingBySchedulingList(daySchedulingList, dayShiftId); //白班包装车间的排产记录生成的包装车间看板
        List<Packing> nightPackingList = createPackingBySchedulingList(nightSchedulingList, nightShiftId); //晚班包装车间的排产记录生成的包装车间看板
        packingList.addAll(dayPackingList);
        packingList.addAll(nightPackingList);
        return packingList;
    }

    /**
     * 注塑车间柱状图
     *
     * @return
     */
    @Override
    public List<Achievement> selectAchievementInfoListForMold() {
        List<Achievement> achievements = bulletinMapper.selectAchievementInfoListForMold();
        for (Achievement a : achievements) {
            a.setAchievementInfoList(bulletinMapper.selectAchievementInfoList(new AchievementInfo(a)));
        }
        return achievements;
    }

    /**
     * 包装车间柱状图
     *
     * @return
     */
    @Override
    public List<Achievement> selectAchievementInfoListForPack() {
        List<Achievement> achievements = bulletinMapper.selectAchievementInfoListForPack();
        for (Achievement a : achievements) {
            a.setAchievementInfoList(bulletinMapper.selectAchievementInfoList(new AchievementInfo(a)));
        }
        return achievements;
    }

    /**
     * 过滤掉没有设备的加工单（包装车间的）
     * @param schedulingList
     * @return
     */
    public List<Scheduling> filterPackingWorkSheet(List<Scheduling> schedulingList) {
        Map<String, Scheduling> map = new HashMap<>();
        for (int i = 0; i < schedulingList.size(); i++) {
            map.put(schedulingList.get(i).getWorkSheetId(), schedulingList.get(i));
        }
        for (int i = 0; i < schedulingList.size(); i++) {
            // 排产，过滤掉有设备的加工单
            if (!schedulingList.get(i).getMachineId().equals("")) {
                // 有设备
                map.remove(schedulingList.get(i).getWorkSheetId());
            }
        }
        return new ArrayList<>(map.values());
    }

    /**
     * 创建包装对象，通过设备排产列表
     * @param schedulingList
     * @return
     */
    public List<Packing> createPackingBySchedulingList(List<Scheduling> schedulingList, String shiftInfoId) {
        List<Packing> packingList = new ArrayList<>();
        for (Scheduling item : schedulingList) {
            Packing packing = new Packing();
            packing.setShiftInfoId(shiftInfoId);
            packing.setWorkSheetId(item.getWorkSheetId());
            ProduceWorkSheet workSheet = workSheetService.selectProduceWorkSheetById(item.getWorkSheetId());
            packing.setProduceName(workSheet.getProduceName());
            packing.setWorkSheetNumber(workSheet.getWorkSheetNumber());
            packing.setProduceNumber(workSheet.getCompletedNumber());
            packing.setStaffName(workSheet.getCreateByName());
            packingList.add(packing);
        }
        return packingList;
    }
}
